package com.hskn.hss.module.videoplan.service.impl;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hskn.hss.constant.CommonConstant;
import com.hskn.hss.core.utils.LoginUtils;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.module.tlequip.entity.TlEquip;
import com.hskn.hss.module.tlequip.mapper.TlEquipMapper;
import com.hskn.hss.module.videofile.entity.VideoFile;
import com.hskn.hss.module.videofile.mapper.VideoFileMapper;
import com.hskn.hss.module.videoplan.entity.VideoPlan;
import com.hskn.hss.module.videoplan.mapper.VideoPlanMapper;
import com.hskn.hss.module.videoplan.service.IVideoPlanService;
import com.hskn.hss.module.videoplan.to.VideoPlanDeleteTO;
import com.hskn.hss.module.videoplan.to.VideoPlanTO;
import com.hskn.hss.utils.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 录像计划
 *
 * @author lny
 * @date 2021-11-18
 */
@Service
public class VideoPlanServiceImpl extends ServiceImpl<VideoPlanMapper, VideoPlan> implements IVideoPlanService {

    @Resource
    private VideoPlanMapper videoPlanMapper;
    @Resource
    private VideoFileMapper videoFileMapper;
    @Resource
    private TlEquipMapper tlEquipMapper;

    @Override
    public List<VideoPlan> list(VideoPlanTO videoPlanTO) {
        String createTimeOrder = videoPlanTO.getCreateTimeOrder();
        if (!"desc".equals(createTimeOrder) && !"asc".equals(createTimeOrder)) {
            throw new RuntimeException("createTimeOrder参数传值错误");
        }
        List<VideoPlan> list = videoPlanMapper.listByParam(videoPlanTO);
        return list;
    }

    @Transactional
    @Override
    public AjaxResult save(VideoPlanTO videoPlanTO) {
        try {
            String uid = LoginUtils.getUser().getUid();
            VideoPlan videoPlan = transferEntity(videoPlanTO, uid);
            videoPlanMapper.insert(videoPlan);

            //保存录像文件
            VideoFile videoFile = new VideoFile();
            videoFile.setId(StringUtils.uuid());
            videoFile.setVideoPlanId(videoPlan.getId());
            videoFile.setStatus(CommonConstant.VIDEO_FILE_STATUS_NOT_START);
            videoFile.setCreateTime(DateUtil.date());
            videoFile.setCreateUserId(uid);
            videoFileMapper.insert(videoFile);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return AjaxResult.fail(e.getMessage());
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult update(VideoPlanTO videoPlanTO) {
        try {
            String uid = LoginUtils.getUser().getUid();
            VideoPlan videoPlan = transferEntity(videoPlanTO, uid);
            videoPlan.setId(videoPlanTO.getId());
            videoPlanMapper.updateById(videoPlan);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return AjaxResult.fail(e.getMessage());
        }
        return AjaxResult.success();
    }

    private VideoPlan transferEntity(VideoPlanTO videoPlanTO, String uid) {
        String equipcode = videoPlanTO.getEquipcode();
        QueryWrapper<TlEquip> queryWrapper = new QueryWrapper<TlEquip>();
        queryWrapper.lambda().eq(TlEquip::getEquipcode, equipcode);
        TlEquip tlEquip = tlEquipMapper.selectOne(queryWrapper);

        Date videoStartTime = DateUtil.parse(videoPlanTO.getVideoDate() + " " + videoPlanTO.getVideoStartTime());
        Date videoEndTime = DateUtil.parse(videoPlanTO.getVideoDate() + " " + videoPlanTO.getVideoEndTime());
        String videoDuration = DateUtil.secondToTime(new Long(DateUtil.between(videoStartTime, videoEndTime, DateUnit.SECOND)).intValue());

        VideoPlan videoPlan = new VideoPlan();
        videoPlan.setId(StringUtils.uuid());
        videoPlan.setEquipcode(videoPlanTO.getEquipcode());
        videoPlan.setBindobjname(null != tlEquip ? tlEquip.getBindobjname() : "");
        videoPlan.setChannel(videoPlanTO.getChannel());
        videoPlan.setRecordTime(videoPlanTO.getRecordTime());
        videoPlan.setVideoStartTime(videoStartTime);
        videoPlan.setVideoEndTime(videoEndTime);
        videoPlan.setVideoDuration(videoDuration);
        videoPlan.setStatus(CommonConstant.VIDEO_PLAN_STATUS_ENABLE);
        videoPlan.setCreateUserId(uid);
        videoPlan.setCreateTime(DateUtil.date());
        return videoPlan;
    }

    @Override
    public AjaxResult delete(VideoPlanTO videoPlanTO) {
        try {
            String uid = LoginUtils.getUser().getUid();
            VideoPlan videoPlan = videoPlanMapper.selectById(videoPlanTO.getId());
            videoPlan.setStatus(CommonConstant.VIDEO_PLAN_STATUS_DELETE);
            videoPlan.setDeleteTime(DateUtil.date());
            videoPlan.setDeleteUserId(uid);
            videoPlanMapper.updateById(videoPlan);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return AjaxResult.fail(e.getMessage());
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult deleteBatch(VideoPlanDeleteTO videoPlanDeleteTO) {
        try {
            String uid = LoginUtils.getUser().getUid();
            String deleteIds = videoPlanDeleteTO.getDeleteIds();
            if (StringUtils.isEmpty(deleteIds)) {
                return AjaxResult.fail("参数为空");
            }
            String[] ids = deleteIds.split(",");
            for (String id : ids) {
                VideoPlan videoPlan = videoPlanMapper.selectById(id);
                videoPlan.setStatus(CommonConstant.VIDEO_PLAN_STATUS_DELETE);
                videoPlan.setDeleteTime(DateUtil.date());
                videoPlan.setDeleteUserId(uid);
                videoPlanMapper.updateById(videoPlan);
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return AjaxResult.fail(e.getMessage());
        }
        return AjaxResult.success();
    }

}
